# encoding: utf-8
# Copyright 2012, Google Inc. All Rights Reserved.
"""
Reset handler
"""
__author__ = 'davidthorpe@google.com'

import sys,os,logging
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from django.utils import simplejson as json

# import local libraries
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))
import constants
import cache

# Create cache
store_cache = cache.StoreCache()
state_cache = store_cache.state

class ResetHandler(webapp.RequestHandler):
	def put(self):
		try:
			# reset the store_cache
			self.response.headers['Content-Type'] = "application/javascript"
			if store_cache.reset_all():
				self.response.out.write(json.dumps(True))
			else:
				self.response.out.write(json.dumps(False))
			self.response.out.write("\n\n")
		except cache.FileNotFound, e:
			self.error(404)
			self.response.headers['Content-Type'] = "text/plain"
			self.response.out.write("Resource not found: %s\n\n" % e)
		except Exception, e:
			self.error(500)
			self.response.headers['Content-Type'] = "text/plain"
			self.response.out.write("Server Error: %s\n\n" % e)

application = webapp.WSGIApplication([
	(r'/reset',ResetHandler)
],debug=constants.DEBUG)

def main():
	if constants.DEBUG:
		logging.getLogger().setLevel(logging.DEBUG)
	else:
		logging.getLogger().setLevel(logging.INFO)
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
